package walletadminlogic

import (
	"context"
	"go.uber.org/zap"
	"navi-mumbai/common/key/dbkey"
	"navi-mumbai/common/utils"

	"navi-mumbai/app/rpc/model/pg/wallet"
	"navi-mumbai/app/rpc/wallet/internal/svc"
	"navi-mumbai/app/rpc/wallet/pb"
	"navi-mumbai/common/key/walletkey"
	"navi-mumbai/common/xerr"

	"github.com/zeromicro/go-zero/core/logx"
)

type QueryWalletMoneyConfigListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewQueryWalletMoneyConfigListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *QueryWalletMoneyConfigListLogic {
	return &QueryWalletMoneyConfigListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 管理后台查询钱包币种配置列表
func (l *QueryWalletMoneyConfigListLogic) QueryWalletMoneyConfigList(in *pb.QueryWalletMoneyConfigListReq) (*pb.QueryWalletMoneyConfigListResp, error) {
	defer utils.RecoverPanic()

	var err error
	var list []*wallet.WalletMoneyConfig
	list, err = l.svcCtx.WalletMoneyConfigModel.Find(l.ctx, dbkey.Enable, in.PageNo, in.PageSize)
	if err != nil {
		l.Logger.Errorf("err == ", zap.Error(err))
		return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
	}

	var rsp pb.QueryWalletMoneyConfigListResp
	rsp.List = make([]*pb.WalletMoneyConfig, 0)
	if len(list) == 0 {
		return &rsp, nil
	}

	if in.PageNo <= 1 {
		rsp.Total, err = l.svcCtx.WalletMoneyConfigModel.Count(l.ctx, dbkey.Enable)
		if err != nil {
			l.Logger.Errorf("err == ", zap.Error(err))
			return nil, xerr.NewGrpcErrCodeMsg(xerr.DbError, err.Error())
		}
	}

	for i := 0; i < len(list); i++ {
		vl := pb.WalletMoneyConfig{
			MoneyType:         list[i].MoneyType,
			MinWithdrawAmount: list[i].MinWithdrawAmount,
			MaxWithdrawAmount: list[i].MaxWithdrawAmount,
			MinDepositAmount:  list[i].MinDepositAmount,
			MaxDepositAmount:  list[i].MaxDepositAmount,
			State:             list[i].State,
			CreateTime:        list[i].CreateTime.Format(dbkey.DateTimeCNFormat),
			UpdateTime:        list[i].UpdateTime.Format(dbkey.DateTimeCNFormat),
			MoneyIcon:         list[i].MoneyIcon,
			MoneyName:         list[i].MoneyName,
		}
		vl.MoneyIcon = walletkey.GetMoneyIcon(list[i].MoneyType)
		rsp.List = append(rsp.List, &vl)
	}
	return &rsp, nil
}
